<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Models\Post;
use App\Models\Comment;
use App\Services\CommentService;
use App\Http\Requests\StoreCommentRequest;
use Illuminate\Support\Facades\Auth;

/**
 * 评论控制器
 * 
 * 职责：
 * - 处理评论相关的HTTP请求
 * - 调用 CommentService 处理业务逻辑
 * - 返回重定向响应
 */
class CommentController extends Controller
{
    /**
     * 评论服务
     * 
     * @var CommentService
     */
    protected CommentService $commentService;

    /**
     * 构造器
     */
    public function __construct(CommentService $commentService)
    {
        $this->commentService = $commentService;
    }

    /**
     * 创建评论
     * 
     * @param StoreCommentRequest $request
     * @param Post $post
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(StoreCommentRequest $request, Post $post)
    {
        // 验证已通过，调用服务层处理业务逻辑
        $comment = $this->commentService->create(
            $post,
            $request->validated(),
            Auth::user()
        );

        return redirect(route('blog.posts.show', $post->slug) . '#comment-' . $comment->id)
            ->with('message', '评论发布成功！');
    }

    /**
     * 删除评论
     * 
     * @param Comment $comment
     * @return \Illuminate\Http\RedirectResponse
     */
    public function destroy(Comment $comment)
    {
        $this->authorize('delete', $comment);
        
        // 调用服务层处理删除逻辑
        $this->commentService->delete($comment, Auth::user());
        
        return back()->with('message', '评论已删除！');
    }
} 